package giit.computerbase.action;

import giit.computerbase.server.DeleteServer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2018-11-29 Thursday 19:02:07
 */
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = (String) request.getSession().getAttribute("user");
        boolean isManager = (boolean) request.getSession().getAttribute("manager");
        if (user == null || user.equals("") || !isManager) {
            response.sendRedirect("index");
        }
        String title = request.getParameter("title");
        String toPath = "warn.jsp?";
        if (title != null) {
            title = URLDecoder.decode(title, "utf-8");
            DeleteServer deleteServer = new DeleteServer();
            if (deleteServer.delete(title)) {
                toPath += ("state=panel-primary&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("删除成功") + "&preview=manager.jsp");
            } else {
                toPath += ("state=panel-danger&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("删除失败") + "&preview=manager.jsp");
            }
        } else {
            toPath += ("state=panel-danger&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("删除失败，信息不完整") + "&preview=manager.jsp");
        }
        response.sendRedirect(toPath);
    }
}
